package top.drinyeit.nongmao.presenter;

import android.location.Location;
import android.util.Log;

import top.drinyeit.nongmao.activity.EditAddressActivity;
import top.drinyeit.nongmao.retrofit.HttpResponse;
import top.drinyeit.nongmao.retrofit.RetrofitMethod;
import top.drinyeit.nongmao.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.nongmao.retrofit.subscriber.SubscriberListener;
import top.drinyeit.nongmao.util.LocationUtils;

import static top.drinyeit.nongmao.app.Constant.COMMON_SUCCESS;

/**
 * Created by bigHead on 2018/1/4.
 */

public class EditAddressPresenter {
    private EditAddressActivity activity;
    String longitude = "";
    String latitude = "";

    public EditAddressPresenter(EditAddressActivity activity){
        this.activity = activity;
    }
    //编辑用户收货地址
    public void editAdd(int address_id,String consignee_name , String consignee_phone,String address_detail){
        activity.showLoading();
        Location location = LocationUtils.getInstance(activity).showLocation();
        if (location == null){
//            activity.showToastMsg("获取地理位置失败,请检查网络/GPS定位后重试");
            longitude = "";
            latitude = "";
        }else {
            String address = "纬度：" + location.getLatitude() + "经度：" + location.getLongitude();
            Log.d( "FLY.LocationUtils", address );
            longitude = location.getLongitude() + "";
            latitude = location.getLatitude() + "";
        }
        CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                activity.dismissLoading();
                activity.showToastMsg(o.getMessage());
                if (o.getCode() == COMMON_SUCCESS){
                    activity.setResult(1);
                    activity.finish();
                }
            }
        });
        RetrofitMethod.getInstance().editAdd(subscriber,address_id,consignee_name,consignee_phone,address_detail,longitude,latitude);
    }
}
